//
//  BaseViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/3.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController (){
    UIButton *_currentButton;
}

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = TABLEVIEW_BACKGROUNDCOLOR;
    
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"PingFang-SC-Bold" size:16.0], nil]];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self != [self.navigationController.viewControllers objectAtIndex:0]) {
        [self setLeftButton:[UIImage imageNamed:@"更多2"] title:nil target:self action:@selector(back) rect:CGRectMake(0, 0, 44, 44)];
        self.tabBarController.tabBar.hidden = YES;
    }else {
        self.tabBarController.tabBar.hidden = NO;
    }
    [self.navigationController.navigationBar setShadowImage:[HttpManager createImageWithColor:REXADECIMALCOLOR(@"#e4e4e4")]];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    for (UIView *field in self.view.subviews) {
        if ([field isKindOfClass:[UITextField class]] || [field isKindOfClass:[UITextView class]]) {
            [field resignFirstResponder];
        }
    }
}

#pragma mark - Method
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIImage *)createImageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

- (UIButton *)setRightButton:(UIImage *)image title:(NSString *)title target:(id)target action:(SEL)selector{
    UIButton *currentButton = [self setRightButton:image title:title target:target action:selector rect:CGRectNull];
    return currentButton;
}

- (UIButton *)setRightButton:(UIImage *)image title:(NSString *)title target:(id)target action:(SEL)selector rect:(CGRect)rect{
    if (self.navigationController && self.navigationItem) {
        CGRect buttonFrame;
        CGRect viewFrame;
        if (CGRectIsNull(rect)) {
            buttonFrame = CGRectMake(0, 0, 44, 44);
            viewFrame = CGRectMake(0, 0, 44, 44);
        } else {
            buttonFrame = rect;
            viewFrame = rect;
        }
        UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
        //        button.imageEdgeInsets = UIEdgeInsetsMake(0, 25, 0, 0);
        if (image) {
            [button setImage:image forState:UIControlStateNormal];
        }
        if (title) {
            [button setTitle:title forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            //Customzied TitleColor..
            //            [button setTitleColor:RGBACOLOR(232, 41, 28, 1.0) forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        }
        [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
        UIView *view = [[UIView alloc] initWithFrame:viewFrame];
        [view addSubview:button];
        _currentButton = button;
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    }
    return _currentButton;
}

- (UIButton *)setLeftButton:(UIImage *)image title:(NSString *)title target:(id)target action:(SEL)selector{
    UIButton *currentButton = [self setLeftButton:image title:title target:target action:selector rect:CGRectNull];
    return currentButton;
}

- (UIButton *)setLeftButton:(UIImage *)image title:(NSString *)title target:(id)target action:(SEL)selector rect:(CGRect) rect{
    if (self.navigationItem && self.navigationController) {
        
        CGRect buttonFrame;
        CGRect viewFrame;
        if (CGRectIsNull(rect)) {
            buttonFrame = CGRectMake(0, 0, 44, 44);
            viewFrame = CGRectMake(0, 0, 44, 44);
        } else {
            buttonFrame = rect;
            viewFrame = rect;
        }
        
        UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
        UIView *view = [[UIView alloc] initWithFrame:viewFrame];
        
        if (image) {
            [button setImage:image forState:UIControlStateNormal];
        }
        if (title) {
            [button setTitle:title forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:16];
            //Customzied TitleColor..
            [button setTitleColor:RGBACOLOR(30, 30, 30, 1.0) forState:UIControlStateNormal];
        }
        [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
        button.imageEdgeInsets = UIEdgeInsetsMake(0, -35, 0, 0);
        [view addSubview:button];
        _currentButton = button;
        
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    }
    return _currentButton;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
